#define _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"

void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = NULL;
	pq->tail = NULL;
}

bool QueueEmpty(Queue* pq)
{
	return (pq->head == NULL && pq->tail == NULL);
}

DataType QueueFront(Queue* pq)
{
	return pq->head->data;
}

int QueueSize(Queue* pq)
{
	int size = 0;
	QueueNode* cur = pq->head;
	while (cur != NULL)
	{
		size++;
		cur = cur->next;
	}
	return size;
}

void QueuePush(Queue* pq,DataType x)
{
	assert(pq);
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	//判断是否为空队列
	assert(newnode);
	if (pq->head == NULL || pq->tail == NULL)
	{
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
	//赋值新结点
	newnode->data = x;
	newnode->next = NULL;
}

void QueuePop(Queue* pq)
{
	assert(pq->head);
	QueueNode* oldhead = pq->head;
	pq->head = pq->head->next;
	free(oldhead);
	//最后判断是否删除的是最后一个元素
	if (pq->head == NULL)
	{
		pq->tail = NULL;
	}
}

void QueueDestroy(Queue* pq)
{
	if (pq->head == NULL)
	{
		return;
	}
	QueueNode* cur = pq->head;
	while (cur != NULL)
	{
		pq->head = pq->head->next;
		free(cur);
		cur = pq->head;
	}
	pq->tail = NULL;
	printf("销毁完毕！\n");
}